<?php 

class Third_Form extends Zend_Form{

	protected $jsValidation = true;
	protected $ajaxSubmit = false;
	protected $ajaxSubmitCallback = false;

	function __construct( $options ){
		parent::__construct( $options );
		
		$plugin_loader = new Zend_Loader_PluginLoader();
		$plugin_loader->addPrefixPath('Third_Validate', 'Third/Validate' );
		$this->addElementPrefixPath( 'Third_Validate', 'Third/Validate', 'VALIDATE' );
		
		$this->setAttrib( 'accept-charset', 'utf8');
	}

	function init(){
		$this->setElementDecorators(array(
		  array('ViewHelper'), //BUG IN Zend_Form_Element_File,
		    array('Errors'),
		    array('Description', array('tag' => 'p', 'class' => 'description')),
		   //  array('HtmlTag', array('tag' => 'li', 'class' => 'element_container') ),
		    array('Label', array('class'=>'label desc')),
	    	array(
				array('row' => 'HtmlTag'), 
	    		array('tag' => 'li', 'class'=>"field_container")
			)
		));
	}

	function setJsValidation( $val ){
		$this->jsValidation = $val;
	}

	function getJsValidation(){
		return $this->jsValidation;
	}

	function setAjaxSubmit( $val ){
		$this->ajaxSubmit = $val;
	}

	function getAjaxSubmit(){
		return $this->ajaxSubmit;
	}
	
	function setAjaxSubmitCallback( $function ){
		return $this->ajaxSubmitCallback = $function;
	}
		
	function getAjaxSubmitCallback(){
		return $this->ajaxSubmitCallback;
	}
	
	function addDefaultClass( $field ){
		$class = $field->getAttrib( 'class' );
		$field->setAttrib('class', $class.' field text full');

		return $field;
	}

	function hasClass( $field, $className ){
		$class = $field->getAttrib( 'class' );

		return strstr($class, $className);
	}
	
	function processJsValidators( $field ){
		$class = $field->getAttrib('class');

		$validateClasses['validate'] = array();
	
		//IF IS REQUIRED
		if( $field->isRequired() ){
			$validateClasses['validate']['required'] = true;
		}

		if( $validateClasses['validate'] ){
			$classes = str_replace('"','', Zend_Json::encode( $validateClasses ) );
			$field->setAttrib('class', $class .' '.$classes );
		}

		return $field;
	}
	
	function addJsValidatorClasses( $elements ){
		//FORM VALIDATIONS
			if( !$this->getId() ){
				$generatedId = 'form_' .uniqid() . '_' . md5(mt_rand()) ;
				$this->setAttrib('id', $generatedId);
			}

		//ADD ELEMENTS CLASSES
		$newElements = array();
		$hasClass = NULL;
		foreach( $elements as $key=>$field){
			//DISABLE HELPER
			$field = $this->disableDecorators( $field );
			
			$field = $this->processJsValidators( $field );
			
			$field->addDecorators(array(
		    	array(
					array('row' => 'HtmlTag'), 
		    		array('tag' => 'li', 'class'=>"field_container ".$field->helper )
				)
			));

			$hasClass .= $this->hasClass( $field, 'tinymce' );
			
			$newElements[$key] = $field;
		}
			
			//VALIDATION TO JQUERY
			$this->getView()->jQuery()->onLoadCaptureStart();
				echo "$('#".$this->getId()."')";
								
				if( $hasClass )
					echo ".submit( function() { tinyMCE.triggerSave(); } )";
				
				echo ".validate({'meta':'validate'";

				
				if( $this->getAjaxSubmit() ){
					echo ", submitHandler: function(form) { ";

					
					echo " $(form).ajaxSubmit({ type: '".$this->getMethod()."'";
						 					
					if( $this->getAjaxSubmitCallback() ){
							echo ", success: ".$this->getAjaxSubmitCallback();
					}
					echo " }) } ";
				}
				echo "}";
					
				echo ");";
			$this->getView()->jQuery()->onLoadCaptureEnd();

			

		
		return $newElements;
	}

	/**
	 * REMOVE ALGUNS DECORATORS DE TIPOS ESPECIFICOS
	 * @param $field
	 * @return unknown_type
	 */
	function disableDecorators( $field ){
			// DISABLE HELPER
			$fieldHelper = ( $field->helper );
			switch( $fieldHelper ){
				case 'formHidden':
					$field->removeDecorator('DtDdWrapper')
						  ->removeDecorator('label')
              			  ->removeDecorator('HtmlTag')
              			  ->removeDecorator('tag')
              			  ->removeDecorator('Description')
              			  ;
				break;
				case 'formSubmit':
					$field->removeDecorator('DtDdWrapper')
							->removeDecorator('label');
				break;
				default:
					$field = $this->addDefaultClass( $field );
				break;
			}
			return $field;
	}

	function render( Zend_View_Interface $view = null ){
		$elements = $this->getElements();

		if( $this->getJsValidation() ){
			$elements = $this->addJsValidatorClasses( $this->getElements() );
		}

		$this->setElements( $elements );

		return parent::render( $view );
	}
}

?>